<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\RobotProduct;
use think\admin\Controller;
use think\facade\Db;

/**
 * 超管系统-Forge管理
 * Class Forge
 * @package app\airobot\controller
 */
class TestForge extends Controller
{
    private $table = 'robot_forge';
    public $commonLogic;

    /**
     * 盒子列表
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = 'Forge列表';
        $query = $this->_query($this->table);
        $where = [];
        if (!empty($_GET['product_id'])) {
            $where['product_id'] = $_GET['product_id'];
        }
        $query->where($where);
        $query->order('id asc')->page();
    }

    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        $ImageUrl = CommonLogic::GetImageUrl();

        // 商品
        $productIds = [];
        foreach ($data as $vo) {
            $productIds[] = $vo["product_id"];
        }
        $productIds = array_unique($productIds);
        $productModel = new RobotProduct();
        $productList = $productModel->queryList([], ['field' => 'id', 'data' => $productIds], [], [], [], "", $field = "*", $order = "");
        $productMap = array_column($productList, null, 'id');
        foreach ($data as &$vo) {
            $vo = $this->dealDecimal($vo);
            $vo['default_spin_amount'] = $this->commonLogic->DealAmount($vo['default_spin_amount']);
            $vo['product_amount'] = isset($productMap[$vo['product_id']]) ? $this->commonLogic->DealAmount($productMap[$vo['product_id']]['amount']) : 0;
            $vo['product_image'] = isset($productMap[$vo['product_id']]) ? $ImageUrl['product'] . $productMap[$vo['product_id']]['image'] : "";
            $vo['head_image'] = $ImageUrl['forge_head'] . $vo['head_image'];
        }
    }

    public function dealDecimal($data)
    {
        foreach ($data as $key => $value) {
            if ($key == "forge_probability" ||
                $key == "min_probability" ||
                $key == "max_probability" ||
                $key == "rtp_base" ||
                $key == "rtp_corr" ||
                $key == "corr_value" ||
                $key == "k_ban" ||
                $key == "default_spin_amount") {
                $data[$key] = floatval($value);
            }
        }
        return $data;
    }
}